/**
 * Package	: com.coship.vasms.adstorm.web.advertiseContent.action
 * File		: AdvertiseContent.java
 * Company 	: Shenzhen Coship Electronics CO., LTD.
 * Version  : v1.0
 * Description 	: <Detail Description for package>
 *
 * Copyright (c) 2003-2009 Shenzhen Coship Electronics CO., LTD.
 * Rainbow Bldg.,North,Hi-Tech Industrial Park,Nanshan District,Shenzhen,China,P.C.518057.
 * All rights reserved.
 * The contents of this file are confidential and proprietary to COSHIP.
 * Copying is explicitly prohibited without the express permission of COSHIP.
 *
 * Create Date	: 2010-5-10
 * Create By    : 904004
 * History :$
 */
package com.coship.vasms.adstorm.web.advertiseContent.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;

import com.coship.vasms.adstorm.common.BeanHelper;
import com.coship.vasms.adstorm.model.ADArrange;
import com.coship.vasms.adstorm.model.Advertisement;
import com.coship.vasms.adstorm.service.ADArrangeService;
import com.coship.vasms.adstorm.service.AdvertisementService;
import com.coship.vasms.adstorm.web.BaseAction;

/** 
 * 
 * File Name    : AdvertiseContent.java
 * @Description 	: 广告内容获取Action<br><br>
 * http://ip:port/advertise/getAdvertise.action? AgentType=xxx?PositionCode=xxx
 * @author 904004
 */
public class AdvertiseContent extends BaseAction {
	/**
	 * Description 	: <Detail Description for package>
	 * Author       : 904004
	 */
	private static final long serialVersionUID = 1L;
	private static final Logger logger = Logger
	.getLogger(AdvertiseContent.class);
	/**
	 * 广告位外码
	 */
	private String positionCode;
	private String agentType;
	/**
	 * 广告排期id
	 */
	private String arrangeId;
	@SuppressWarnings("unchecked")
	public void getAdvertise(){
		boolean isRealTime = false;//是否是实时获取（根据排期时间判断）||否（根据排期id获取广告）
		boolean paramNull = false;//参数空，错误
		if( (positionCode != null && !positionCode.equals("")) 
				&& (arrangeId != null && !arrangeId.equals("") )){
			//如果两个参数都有，则根据排期id查询广告
			isRealTime = false;
		}else if(positionCode != null && !positionCode.equals("")){
			isRealTime = true;
		}else if(arrangeId != null && !arrangeId.equals("")){
			isRealTime = false;
		}else{
			paramNull = true;
		}
		try {
			if(!paramNull){
				ADArrangeService adas = (ADArrangeService)BeanHelper.getInstance().getBean("adarrangeService");
				AdvertisementService<Advertisement> ads = (AdvertisementService<Advertisement>)BeanHelper.getInstance().getBean("advertisementService");
				ADArrange ada = null;
				if(isRealTime){//实时获取
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
					String time = sdf.format(new Date());
					ada = adas.queryByPositionBasicCodeAndTime(positionCode, time);
				}else{//根据排期id获取
					int id =0;
					try {
						 id = Integer.parseInt(arrangeId);
					} catch (Exception e) {
					}
					 ada = adas.findById(id);
				}
				
				int advertiseId = (ada == null)?0:ada.getAdvertiseId();
				Advertisement advertise = ads.queryAdvertisementById(advertiseId);
				int advertiseType = (advertise == null)?0:advertise.getAdvertiseType();//广告类型1:文字 2：图片
				if(advertiseType == 2){//直接返回图片
					  HttpServletResponse response =  ServletActionContext.getResponse();
					  response.setContentType("image/jpeg");
					  String path = advertise.getContent();
					  File f = new File(path);
					  if(!f.isFile()){
						  throw new Exception("图片路径出错");
					  }
					  InputStream is=new FileInputStream(f);
					  int size=is.available();
					  byte[] image=new byte[size];
					  is.read(image);
					  ServletOutputStream out=response.getOutputStream();
					  out.write(image);
				}else if(advertiseType == 1){
					String content = advertise.getContent();
					HttpServletResponse response =  ServletActionContext.getResponse();
					response.setCharacterEncoding("UTF-8");
					PrintWriter out = response.getWriter();
					out.write(content);
				}else{//无广告
					PrintWriter out = ServletActionContext.getResponse().getWriter();
					out.write("0");
				}
				
			}else{
				//null
				PrintWriter out = ServletActionContext.getResponse().getWriter();
				out.write("0");
			}
		} catch (Exception e) {
			logger.error(e.getMessage());
			logger.error("获取广告内容出错", e);
		}
	}
	public String getPositionCode() {
		return positionCode;
	}
	public void setPositionCode(String positionCode) {
		this.positionCode = positionCode;
	}
	public String getAgentType() {
		return agentType;
	}
	public void setAgentType(String agentType) {
		this.agentType = agentType;
	}
	public String getArrangeId() {
		return arrangeId;
	}
	public void setArrangeId(String arrangeId) {
		this.arrangeId = arrangeId;
	}
}
